using System;
using Core;
using Microsoft.Xna.Framework;
using SolarWinds.Animations;
using SolarWinds.Layouts;
using SolarWinds.Widgets;

namespace SolarWinds.Screens
{
    public class MainMenuScreen : Screen
    {
        private readonly WidgetManager mWidgetManager;
        private IScreenManagerService mScreenManager;

        private Window mWindow;
        private PushButton mSingleplayerButton;
        private PushButton mOptionsButton;
        private PushButton mMultiplayerButton;
        private PushButton mCreditsButton;
        private PushButton mExitButton;

        public MainMenuScreen(Game game)
            : base(game)
        {
            mWidgetManager = new WidgetManager(game);
            Components.Add(mWidgetManager);

            BlocksDraw = false;
            BlocksUpdate = false;
        }

        public override void Initialize()
        {
            mScreenManager = (IScreenManagerService)
                             Game.Services.GetService(typeof(IScreenManagerService));
            
            base.Initialize();

            #region Widgets

            mWindow = new Window(Game)
                          {
                              Position = new Point(20, 170),
                              Caption = "DeepSpace v0.1.1",
                              Width = 200,
                              Height = 220,
                              Moveable = true,
                              Resizeable = true,
                              HasCloseButton = true,
                              Tabable = false
                          };
            mWidgetManager.AddChild(mWindow);

            mSingleplayerButton = new PushButton(Game) { Caption = "Singleplayer" };
            mMultiplayerButton = new PushButton(Game) {Caption = "Multiplayer"};
            mOptionsButton = new PushButton(Game) {Caption = "Options"};
            mCreditsButton = new PushButton(Game) {Caption = "Credits"};
            mExitButton = new PushButton(Game) {Caption = "Exit"};

            mWindow.Layout = new BoxLayout(Direction.TopToBottom, mWindow)
                                 {
                                     Spacing = 20
                                 };

            mWindow.AddChild(mSingleplayerButton);
            mWindow.AddChild(mMultiplayerButton);
            mWindow.AddChild(mOptionsButton);
            mWindow.AddChild(mCreditsButton);
            mWindow.AddChild(mExitButton);

            mSingleplayerButton.Clicked += mSingleplayerButton_Clicked;
            mMultiplayerButton.Clicked += new EventHandler(mMultiplayerButton_Clicked);
            mOptionsButton.Clicked += OptionsButtonClicked;
            mCreditsButton.Clicked += CreditsButtonClicked;
            mExitButton.Clicked += ExitButtonClicked;

            #endregion
        }

        void  mMultiplayerButton_Clicked(object sender, EventArgs e)
        {
            mScreenManager.PopScreen(true);
            mScreenManager.PushScreen(new LobbyScreen(Game));
        }

        void  mSingleplayerButton_Clicked(object sender, EventArgs e)   
        {
            mScreenManager.ClearScreens();
            mScreenManager.PushScreen(new GameScreen(Game));
        } 
        
        void CreditsButtonClicked(object sender, EventArgs e)
        {
            mScreenManager.PopScreen();
        } 

        public override void  RemoveEventHandlers()
        {
            mExitButton.Clicked -= ExitButtonClicked;
            mOptionsButton.Clicked -= OptionsButtonClicked;
            mSingleplayerButton.Clicked -= mSingleplayerButton_Clicked;
            mMultiplayerButton.Clicked -= mMultiplayerButton_Clicked;
        }

        void OptionsButtonClicked(object sender, EventArgs e)
        {
            mScreenManager.PushScreen(new OptionsScreen(Game));
        }

        void ExitButtonClicked(object sender, EventArgs e)
        {
            Game.Exit();
        }

    }
}